﻿using System.Security.Cryptography;
using System.Text;

namespace MyDocuments.Shared
{
    public class Utils
    {
        public static string OpenAiKey = "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";
        public static string BasePath = "J:\\Web_Shop";
        public static string BasePwd = "";
        public static List<FileNames> LastResult = new List<FileNames>();

        //获得指定路径下所有文件名
        public static List<FileNames> getFileName(List<FileNames> list, string filepath)
        {
            DirectoryInfo root = new DirectoryInfo(filepath);
            foreach (FileInfo f in root.GetFiles())
            {
                var file = new FileNames
                {
                    Name = f.Name,
                    Path = f.FullName.Replace(BasePath, "")
                };
                list.Add(file);
                file.Pwd = Utils.ConvertMd5(file.Path + Utils.BasePwd);
                LastResult.Add(file);
            }
            return list;
        }    
        public static List<FileNames> GetallDirectory(List<FileNames> list, string path)
        {
            DirectoryInfo root = new DirectoryInfo(path);
            var dirs = root.GetDirectories();
            if (dirs.Count() != 0)
            {
                foreach (DirectoryInfo d in dirs)
                {
                    if (d.Name.StartsWith('.'))
                        continue;

                    var childs = GetallDirectory(new List<FileNames>(), d.FullName);

                    list.Add(new FileNames
                    {
                        Name = d.Name,
                        Path = d.FullName.Replace(BasePath,""),
                        isDirectory = true,
                        Children = childs
                    });
                }
            }
            list = getFileName(list, path);
            return list;
        }
        public static List<FileNames> getFileNameBySearch(List<FileNames> list, string filepath, string search)
        {
            DirectoryInfo root = new DirectoryInfo(filepath);
            foreach (FileInfo f in root.GetFiles())
            {
                var content = ( File.ReadAllText(f.FullName)  ?? "") .ToLower();
                if (content.Contains(search) || f.Name.ToLower().Contains(search)) {
                    list.Add(new FileNames
                    {
                        Name = f.Name,
                        Path = f.FullName.Replace(BasePath, "")
                    });
                }
            }
            return list;
        }
        public static List<FileNames> GetallDirectoryBySearch(List<FileNames> list, string path, string search)
        {
            DirectoryInfo root = new DirectoryInfo(path);
            var dirs = root.GetDirectories();
            if (dirs.Count() != 0)
            {
                foreach (DirectoryInfo d in dirs)
                {
                    if (d.Name.StartsWith('.'))
                        continue;

                    var childs = GetallDirectoryBySearch(new List<FileNames>(), d.FullName, search);

                    if (childs.Any()) {
                        list.Add(new FileNames
                        {
                            Name = d.Name,
                            Path = d.FullName.Replace(BasePath, ""),
                            isDirectory = true,
                            Children = childs
                        });
                    }
                    else if (d.Name.ToLower().Contains(search))
                    {
                        list.Add(new FileNames
                        {
                            Name = d.Name,
                            Path = d.FullName.Replace(BasePath, ""),
                            isDirectory = true,
                            Children = childs
                        });
                    }

                    
                }
            }
            list = getFileNameBySearch(list, path, search);
            return list;
        }

        public static FileNames? GetFileByPwd(string pwd)
        {
           return LastResult.Where(item => item.Pwd == pwd).FirstOrDefault();
        }
        public static string ConvertMd5(string val)
        {
            var buffers =
                    MD5.Create()
                    .ComputeHash(Encoding.UTF8.GetBytes(val));

            StringBuilder sc = new StringBuilder();
            for (int i = 0; i < buffers.Length; i++)
            {
                sc.Append(buffers[i].ToString("x2"));
            }

            return sc.ToString();
        }
    }
}
